package com.buba.personnel.controller;

import com.alibaba.fastjson.JSON;
import com.buba.personnel.pojo.Pagination;
import com.buba.personnel.pojo.admin;
import com.buba.personnel.pojo.department;
import com.buba.personnel.service.administerService;
import com.buba.personnel.service.departmentService;
import com.buba.personnel.service.impl.administerServiceImpl;
import com.buba.personnel.service.impl.departmentServiceImpl;
import com.buba.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/administerServlet")
public class administerServlet extends BaseServlet {
    administerService administerService = new administerServiceImpl();
    //部门
    departmentService departmentService =  new departmentServiceImpl();
    @Override
    public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String flag = request.getParameter("flag");
        if ("selDep".equals(flag))
            this.selAdmin(request,response);
        if ("addDep".equals(flag))
            this.addAdmin(request,response);
        if ("updDep".equals(flag))
            this.updAdmin(request,response);
        if ("delDep".equals(flag))
            this.delAdmin(request,response);
    }

    /**
     * 删除
     * @param request
     * @param response
     */
    private void delAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println("删除");
        Map<String, String> param = getParam(request);
        int count = administerService.delAdmin(param);
        response.getWriter().write(count+"");
    }

    /**
     * 修改
     * @param request
     * @param response
     */
    private void updAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println("修改");
        Map<String, String> param = getParam(request);
        int count =  administerService.updAdmin(param);
        //返回数据
        response.getWriter().write(count+"");
    }

    /**
     * 添加方法
     * @param request
     * @param response
     */
    private void addAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println("添加");
        Map<String, String> param = getParam(request);
//        for(String key:param.keySet()){
//            System.out.println("key:"+key+" "+"Value:"+param.get(key));
//        }
        administerService.insert(param);
        response.sendRedirect("buba/personnel/administer.html");

    }

    /**
     * 查询方法
     * @param request
     * @param response
     */
    private void selAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println("查询");
        // 获取查询携带的参数 分页参数 查询条件参数
        Map<String, String> param = getParam(request);
        // 分页工具类
        Pagination<admin> pagination = new Pagination<>(Integer.parseInt(param.get("page")));
        pagination.setParam(param);
        // 1、查询员工的总条数
        pagination.setCount(administerService.selCount(pagination));
        // 2、查询当前页的员工信息
        pagination.setList(administerService.selAdmin(pagination));
        //部门查询
        Map<String, String> param2 = new HashMap<>();
        param2.put("page","1");
        param2.put("limit","5");
        List<department> list1 = departmentService.selDep(param2);
        //用于装返回给浏览器的数据
        Map<String, Object> map = new HashMap<>();
        map.put("list1",list1);
        map.put("list",pagination);
        // 最后返回浏览器
        response.getWriter().write(JSON.toJSONString(map));
    }
}
